import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import addRouterData from "./rootData";
Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/login.vue"),
    meta: {
      title: "登录",
      ispublic: true,
    },
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: {
      title: "首页",
      ispublic: true,
      uuid: "123123123",
    },
    children: [
      {
        path: "dataMaintain/echarts",
        name: "echarts",
        meta: {
          title: "echars图表",
          uuid: "123123123echars",
        },
        component: () => import("../views/dataMaintain/echarts.vue"),
      },
      {
        path: "dataMaintain/pointLine",
        name: "pointLine",
        meta: {
          title: "pointLine",
          uuid: "123123123pointLine",
        },
        component: () =>
          import("../views/dataMaintain/pointLine/pointLine.vue"),
      },
      {
        path: "dataMaintain/echartsTree",
        name: "echartsTree",
        meta: {
          title: "echartsTree",
          uuid: "123123123pointLine",
        },
        component: () => import("../views/dataMaintain/echartsTree.vue"),
      },
      {
        path: "3d/3d",
        name: "3dTree",
        meta: {
          title: "3dtree",
          uuid: "123123123echars",
        },
        component: () => import("../views/3d/3d.vue"),
      },
      {
        path: "3d/card",
        name: "3dcard",
        meta: {
          title: "3dtree",
          uuid: "1231231223echars",
        },
        component: () => import("../views/3d/card.vue"),
      },
      {
        path: "3d/car",
        name: "3dcar",
        meta: {
          title: "car",
          uuid: "123123122323echars",
        },
        component: () => import("../views/3d/car.vue"),
      },
      {
        path: "3d/img3d",
        name: "img3d",
        meta: {
          title: "img3d",
          uuid: "123123122323img3d",
        },
        component: () => import("../views/3d/img3d.vue"),
      },
      {
        path: "/home/dataMaintain/dataMaintain",
        name: "dataMaintain",
        component: () => import("../views/dataMaintain/dataMaintain.vue"),
      },
      {
        path: "ruleSetting/termSetting",
        name: "termSetting",
        component: () => import("../views/caregories/termSetting.vue"),
      },
      {
        path: "ruleSetting/ruleSetting",
        name: "termSetting",
        component: () => import("../views/caregories/ruleSetting.vue"),
      },
      {
        path: "ruleEngine/dataFactory",
        name: "dataFactory",
        component: () => import("../views/ruleEngine/dataFactory.vue"),
      },
      {
        path: "ruleEngine/dataCenter",
        name: "dataCenter",
        component: () => import("../views/ruleEngine/dataCenter.vue"),
      },
      {
        path: "systemManagement/fileManagement",
        name: "systemManagement",
        component: () => import("../views/systemManagement/fileManagement.vue"),
      },
      {
        path: "systemManagement/printPdfDom",
        name: "printPdfDom",
        component: () => import("../views/systemManagement/printPdfDom.vue"),
      },
      {
        path: "systemManagement/wordSystem",
        name: "wordSystem",
        component: () => import("../views/systemManagement/wordSystem.vue"),
      },
      {
        path: "systemManagement/wsChart",
        name: "wsChart",
        component: () =>
          import("../views/systemManagement/wsChart/wsChart.vue"),
      },
      {
        path: "systemManagement/codeGenerator",
        name: "codeGenerator",
        component: () =>
          import("../views/systemManagement/codeGenerator/codeGenerator.vue"),
      },
      {
        path: "projectOne",
        name: "projectOne",
        component: () => import("../views/messagework/project.vue"),
        children: [
          {
            path: "echarsHome",
            name: "echarsHome",
            component: () => import("../views/messagework/echarsHome.vue"),
          },
          {
            path: "workManagement",
            name: "workManagement",
            component: () => import("../views/messagework/workManagement.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/dataMaintain/worldMap",
    name: "worldMap",
    meta: {
      title: "worldMap",
      uuid: "123123123worldMap",
    },
    component: () => import("../views/dataMaintain/worldMap/worldMap2.vue"),
  },
  {
    path: "/flowChart",
    name: "flowChart",
    component: () => import("../views/flowChart.vue"),
    meta: {
      ispublic: true,
    },
  },
  {
    path: "/editVisual",
    name: "editVisual",
    component: () => import("../views/visual"),
    meta: {
      ispublic: true,
    },
  },
  {
    path: "/assembly",
    name: "assembly",
    component: Home,
    children: [
      {
        path: "folderCard",
        name: "folderCard",
        meta: {
          title: "folderCard",
          uuid: "11123folderCard",
        },
        component: () => import("../views/assembly/folderCard.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});
// router.beforeEach((to, form, next) => {
//         if ((!to.meta.ispublic && !sessionStorage.token) || (to.name === "Home")) {
//             console.log("reee", to.meta.ispublic, sessionStorage.token)
//             return next('/login')
//         }
//         next()

//     })
// 全局路由守卫，如果不是登录状态，则到登录页面  路由拦截
router.beforeEach((to, from, next) => {
  // 有token为登录状态
  if (sessionStorage.token) {
    // 动态添加路由数据
    // 接口获取路由信息存储至状态机内， 启路思想为前端写全路由 接口返回路由名 icon uuid path，前端meta中也需要写uuid，对比uuid判断是否有权限
    // 可以在此处获取 nav 数据存储至状态机内
    //router.addRoute(data)
    // 拦截函数
    addRouterData(to).then((res) => {
      // 根据拦截函数判定是否有权限，如果没有跳404页面
      next();
    });
  } else {
    if (to.path === "/login") {
      next();
    } else {
      next("/login");
    }
  }
});
export default router;
